#!/bin/bash
# This is my service script
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DAEMON="/usr/bin/broadcast"
DAEMON_ARGS="-c -t -1 --keep-alive --name linux" # Replace with the path to your daemon executable
NAME="brocli" # Name of your service
DESC="broadcast client server" # Description of your service
PIDFILE="/var/run/$NAME.pid" # Path where PID file will be stored
LOGFILE="/var/log/$NAME.log" # Path where log file will be written
USER=root # User that runs the service (change as needed)
GROUP=root # Group that owns the service (change as needed)
 
start() {
    if [ -f "$PIDFILE" ]; then
        echo "$NAME is already running."
        exit 0
    fi
    
    echo "Starting $DESC: $NAME..."
    start-stop-daemon --start --make-pidfile --pidfile "$PIDFILE" --chuid "$USER:$GROUP" \
                  --exec "$DAEMON" -- $DAEMON_ARGS 1>>"$LOGFILE" 2>&1 &
    echo "$NAME started."
}
 
stop() {
    if ! [ -f $PIDFILE ]; then
        echo "$NAME not running!"
        exit 0
    fi
    
    echo "Stopping $NAME..."
    kill $(cat $PIDFILE); rm -rf $PIDFILE;
}
 
restart() {
    stop
    sleep 3
    start
}

status() {
    PID=`ps -ef |grep $NAME|grep -v grep|wc -l`
    if [ $PID != 0 ];then
        echo "$NAME is running..."
    else
        echo "$NAME is not running..."
    fi
}

 
case "$1" in
    start)
        start;;
    stop)
        stop;;
    restart|reload)
        restart;;
    status)
        status;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart}" >&2
        exit 1;;
esac
exit 0